//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Workflow.ComponentModel;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Http Data Reader Async")]
    [Description("Given a list of URL's this method downloads files from those locations.")]
    [WorkflowCategory("Web")]
    public class DownloadTCPData : Activity
    {
        public static DependencyProperty ServerIPProperty = DependencyProperty.Register("ServerIP",
            typeof(string), typeof(DownloadTCPData));

        [RequiredInputParam]
        [Name("ServerIP")]
        [Description("IP address to the TCP server accepting client requests.")]
        public string ServerIP
        {
            get
            {
                return ((string)(base.GetValue(DownloadTCPData.ServerIPProperty)));
            }
            set
            {
                base.SetValue(DownloadTCPData.ServerIPProperty, value);
            }
        }

        public static DependencyProperty PortProperty = DependencyProperty.Register("Port",
            typeof(string), typeof(DownloadTCPData));

        [RequiredInputParam]
        [Name("Port")]
        [Description("Port number on which the server is listening.")]
        public string Port
        {
            get
            {
                return ((string)(base.GetValue(DownloadTCPData.PortProperty)));
            }
            set
            {
                base.SetValue(DownloadTCPData.PortProperty, value);
            }
        }

        public static DependencyProperty DataProperty = DependencyProperty.Register("Data",
            typeof(string), typeof(DownloadTCPData));

        [RequiredInputParam]
        [Name("Data")]
        [Description("Data downaloded from the TCP server as string.")]
        public string Data
        {
            get
            {
                return ((string)(base.GetValue(DownloadTCPData.DataProperty)));
            }
            set
            {
                base.SetValue(DownloadTCPData.DataProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                int port = Convert.ToInt32(this.Port);
                TcpClient tcpClient = new TcpClient(this.ServerIP, port);
                NetworkStream nStream = tcpClient.GetStream();

                StreamReader readImageData = new StreamReader(nStream);
                StringBuilder BlockData1 = new StringBuilder();

                string data;
                bool Done = false;

                while (Done == false)
                {
                    while ((data = readImageData.ReadLine()) != null)
                    {
                        BlockData1.Append(data);
                    }
                    Done = true;
                }

                this.Data = BlockData1.ToString();
                readImageData.Close();
                tcpClient.Close();

                Console.WriteLine("Transfer Complete");
            }
            catch (Exception er)
            {
                Console.WriteLine("Unable to connect to server");
                Console.WriteLine(er.Message);
            }
            return base.Execute(executionContext);
        }
    }
}
